package com.tg.demo.seata.order.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.tg.demo.seata.order.entity.SeataOrder;
import com.tg.demo.seata.order.feign.StorageFeignClient;
import com.tg.demo.seata.order.mapper.SeataOrderMapper;
import com.tg.demo.seata.order.service.ISeataOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Rubble
 * @since 2020-05-22
 */
@Service
public class SeataOrderServiceImpl extends ServiceImpl<SeataOrderMapper, SeataOrder> implements ISeataOrderService {

    @Resource
    private StorageFeignClient storageFeignClient;



    @Override
    @Transactional(rollbackFor = Exception.class)
    @GlobalTransactional
    public void seataSaveOrder(SeataOrder seataOrder) {
        save(seataOrder);
        storageFeignClient.deductStorage(1,seataOrder.getCount());
//        throw new RuntimeException("seataSave ex");
        boolean randomBoolean = RandomUtil.randomBoolean();
        if(!randomBoolean){
            throw new RuntimeException("seataSaveOrder ex");
        }
    }

}
